package com.itheima.pojo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateCompareUtils {
    public static void main(String[] args) {
        DateCompare dateCompare = DateCompare("2018-12-30", "2019-03-25");
        System.out.println("dateCompare = " + dateCompare);
    }
    public static DateCompare DateCompare(String startDateStr, String endDateStr){
        SimpleDateFormat strmatter=new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = null;
        Date endDate = null;
        try {
            startDate = strmatter.parse(startDateStr);
             endDate = strmatter.parse(endDateStr);
        } catch (
                ParseException e) {
            e.printStackTrace();
        }
        Calendar gregorianCalendar=new GregorianCalendar();
        Calendar gregorianCalendar1=new GregorianCalendar();
        gregorianCalendar.setTime(startDate);
        gregorianCalendar1.setTime(endDate);
        int startYear=gregorianCalendar.get(Calendar.YEAR);//获得年
        System.out.println("startYear = " + startYear);
        int endYear=gregorianCalendar1.get(Calendar.YEAR);//获得年
        System.out.println("endYear = " + endYear);
        int startMonth=gregorianCalendar.get(Calendar.MONTH)+1;//月
        System.out.println("startMonth = " + startMonth);
        int endMonth=gregorianCalendar1.get(Calendar.MONTH)+1;//月
        System.out.println("endMonth = " + endMonth);
        int startDate1=gregorianCalendar.get(Calendar.DATE);//日
        int endDate1=gregorianCalendar1.get(Calendar.DATE);//日
        if (startYear > endYear) {
            int t = startYear;
            endYear = endYear;
            endYear = t;
        }
        if(startMonth>endMonth){
            int t = startMonth;
            startMonth = endMonth;
            endMonth = t;
        }
        DateCompare dateCompare = new DateCompare();
        if(endMonth>startMonth){
            dateCompare.setdValueMonth((endYear-startYear)*12+endMonth-startMonth);
        }else {
            dateCompare.setdValueMonth((endYear-startYear)*12-endMonth+startMonth+1);

        }

        dateCompare.setEndMonth(endMonth);
        dateCompare.setEndYear(endYear);
        dateCompare.setStartMonth(startMonth);
        dateCompare.setStartYear(startYear);
        dateCompare.setStartDate(startDate1);
        dateCompare.setEndDate(endDate1);
        return dateCompare;
    }

}
